package dyy.zc.alumnibook.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;

import dyy.zc.alumnibook.R;
import dyy.zc.alumnibook.base.BaseActivity;
import dyy.zc.alumnibook.bean.FriendCircle;
import dyy.zc.alumnibook.bean.PhotoAlbum;
import dyy.zc.alumnibook.bean.UserInfo;
import dyy.zc.alumnibook.ui.adapter.FriendCircleAdapter;
import dyy.zc.alumnibook.utils.BmobUtils;
import dyy.zc.alumnibook.utils.ImageLoaderUtils;
import dyy.zc.alumnibook.views.CircleImageView;
import dyy.zc.alumnibook.views.EmptyView;

/**
 * Created by yyuand on 2017.4.22.
 */

public class FriendHomePageActivity extends BaseActivity implements View.OnClickListener {
    private final int LIST_SUCCESS = 1;
    private final int SUCCESS = 2;
    public List<FriendCircle> list = new ArrayList<>();
    public PhotoAlbum photoAlbum;
    public UserInfo userInfo;
    private TextView title,name,phone;
    private RelativeLayout back;
    private CircleImageView head;
    private ImageView change;
    private BmobUtils bmobUtils;
    private PullToRefreshListView pfl;
    private ListView listView;
    private FriendCircleAdapter adapter;
    private EmptyView emptyView;
    private String id;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case SUCCESS:
                    setData(userInfo);
                    Bundle bundle = msg.getData();
                    List<FriendCircle> friendCircles = (List<FriendCircle>) bundle.getSerializable("list");
                    list.addAll(friendCircles);
                    break;
                case LIST_SUCCESS:
                    Bundle bundle1 = msg.getData();
                    list.clear();
                    List<FriendCircle> friendCircles1 = (List<FriendCircle>) bundle1.getSerializable("list");
                    list.addAll(friendCircles1);
                    changeData();
                    break;
            }
        }
    };

    /**
     * 添加数据
     */
    private void changeData() {
        //判断如果没有数据的话，则显示空提示
        if (list.size() == 0) {
            emptyView.setNotify("暂无动态");
        } else {
            emptyView.setEmptyViewGone();
        }
        adapter.setList(list);
        adapter.notifyDataSetChanged();
        pfl.onRefreshComplete();
    }

    @Override
    protected void findViewByIDS() {
        bmobUtils = new BmobUtils(this);
        pfl = findViewsById(R.id.recyclerView);
        pfl.setMode(PullToRefreshBase.Mode.PULL_DOWN_TO_REFRESH);
        listView = pfl.getRefreshableView();

        emptyView = new EmptyView(this);

        back = findViewsById(R.id.top_rl_back);
        change = findViewsById(R.id.change);
        change.setVisibility(View.GONE);
        title = findViewsById(R.id.top_tv_title);
        name = findViewsById(R.id.name);
        phone = findViewsById(R.id.phone);
        head = findViewsById(R.id.head);
        title.setText("我的动态");
        back.setVisibility(View.GONE);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_mine);
        initData();
        bind();
    }

    private void bind() {
        change.setOnClickListener(this);
        findViewsById(R.id.checkphoto).setOnClickListener(this);
        findViewsById(R.id.photo).setVisibility(View.GONE);
        adapter = new FriendCircleAdapter(this, list, getVmWidth());
        listView.setAdapter(adapter);

        pfl.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                bmobUtils.queryPersonalCircle(userInfo,LIST_SUCCESS,handler);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

            }
        });
    }

    private void initData() {
        bmobUtils = new BmobUtils(this);
        id = getIntent().getStringExtra("account");
        bmobUtils.findFriendInfo(id,SUCCESS,handler);
        bmobUtils.queryPersonalCircle(userInfo,LIST_SUCCESS,handler);
        TextView photo = findViewsById(R.id.checkphoto);
        photo.setText("他(她)的相册");
        adapter = new FriendCircleAdapter(this, list, getVmWidth());
    }

    private void setData(UserInfo userInfo) {
        ImageLoaderUtils.initImage(this,userInfo.getHead().getFileUrl(),head,R.drawable.chat_xe_icon2_03);
        name.setText(userInfo.getName());
        phone.setText(userInfo.getAccount());
        Log.e("name",userInfo.getName());
        Log.e("phone",userInfo.getAccount());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.checkphoto:
                Intent intent = new Intent(FriendHomePageActivity.this, CheckPhotoActivity.class);
                intent.putExtra("id",1);
                intent.putExtra("photo",userInfo);
                startActivity(intent);
                break;
        }

    }
}
